*************************************
*** GENDER GAP ANALYSIS *************
*** UNWEIGHTED BIVARIATE ANALYSES ***
*** QUINN M. ALBAUGH ****************
*** NOVEMBER 2023 VERSION ***********
*************************************

*** SET WORKING DIRECTORY
clear all

*** CHANGE SETTINGS FOR GRAPHS
set more off, permanently
grstyle init
grstyle set plain, horizontal nogrid
graph set window fontface "Helvetica"

*** IMPORT DATA
use "${here}GenderGapAnalysis.dta", clear

*************************************
*** BIVARIATE RELATIONSHIP GRAPHS ***
*************************************

gen year = 2021

drop if genderid == 4

recode partyid (7=6)
recode partyid (8=7)
label define parties3 1 "Liberal" 2 "Conservative" 3 "NDP" 4 "BQ" 5 "Green" 6 "Other" 7 "None"
label values partyid parties3

tab partyid, gen(pid_)

recode pt (12=11) (13=11)

************************
*** UNWEIGHTED, 2021 ***
************************

****************************
*** PARTY IDENTIFICATION ***
****************************

*** IN-TEXT REFERENCES: UNWEIGHTED GENDER GAPS
tab genderid partyid, row
tab genderid voteintention, row

*** IN-TEXT REFERENCES: WEIGHTED GENDER GAPS
tab genderid partyid [aweight=cps21_weight_general_all], row
tab genderid voteintention [aweight=cps21_weight_general_all], row

tab partyid [aweight=cps21_weight_general_all]
tab voteintention [aweight=cps21_weight_general_all]

*** LIBERAL
preserve
keep if year == 2021 & genderid ~= .
drop if pid_1 == .

tab genderid pid_1, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen liberal_sum = sum(pid_1)
by genderid, sort: gen p_hat = liberal_sum / N

by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Liberal", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 75) titlegap(2)) ///
	ylabel(0(25)75) ///
	yline(33.23, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_LIB_U, replace
restore

*** CONSERVATIVE
preserve
keep if year == 2021 & genderid ~= .
drop if pid_2 == .

tab pid_2

tab genderid pid_2, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen conservative_sum = sum(pid_2)
by genderid, sort: gen p_hat = conservative_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Conservative", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 75) titlegap(2)) ///
	ylabel(0(25)75) ///
	yline(24.93, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_CON_U, replace
restore

*** NDP
preserve
keep if year == 2021 & genderid ~= .
drop if pid_3 == .

tab pid_3

tab genderid pid_3, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen ndp_sum = sum(pid_3)
by genderid, sort: gen p_hat = ndp_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("NDP", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 75) titlegap(2)) ///
	ylabel(0(25)75) ///
	yline(16.18, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_NDP_U, replace
graph export "NDPPID_U.png", height(1200) width(1600) replace
restore

*** BQ
preserve
keep if year == 2021 & genderid ~= .
drop if pid_4 == .

tab pid_4

tab genderid pid_4, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen ndp_sum = sum(pid_4)
by genderid, sort: gen p_hat = ndp_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("BQ", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 75) titlegap(2)) ///
	ylabel(0(25)75) ///
	yline(9.52, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_BQ_U, replace
restore

*** GREEN
preserve
keep if year == 2021 & genderid ~= .
drop if pid_5 == .

tab pid_5

tab genderid pid_5, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen green_sum = sum(pid_5)
by genderid, sort: gen p_hat = green_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Green", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 75) titlegap(2)) ///
	ylabel(0(25)75) ///
	yline(2.92, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_GRN_U, replace
restore

*** NONE
preserve
keep if year == 2021 & genderid ~= .
drop if pid_7 == .

tab pid_7

tab genderid pid_7, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen green_sum = sum(pid_7)
by genderid, sort: gen p_hat = green_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("None", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 75) titlegap(2)) ///
	ylabel(0(25)75) ///
	yline(10.95, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_NONE_U, replace
restore

*** COMBINE PARTY IDENTIFICATION GRAPHS
graph combine PID_LIB_U.gph PID_CON_U.gph PID_NDP_U.gph PID_BQ_U.gph PID_GRN_U.gph PID_NONE_U.gph, col(2) ycommon xcommon b1("Gender Identity", size(small)) l1("Percent Identifying with Party", size(small))
graph export "GenderPID_Unweighted.png", height(2400) width(2000) replace

***********************
*** VOTE INTENTIONS ***
***********************

*** LIBERAL
preserve
keep if year == 2021 & genderid ~= .
drop if vote_1 == .

tab vote_1

tab genderid vote_1, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen liberal_sum = sum(vote_1)
by genderid, sort: gen p_hat = liberal_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Liberal", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 75) titlegap(2)) ///
	ylabel(0(25)75) ///
	yline(31.16, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_LIB_U, replace
restore

*** CONSERVATIVE
preserve
keep if year == 2021 & genderid ~= .
drop if vote_2 == .

tab vote_2

tab genderid vote_2, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen conservative_sum = sum(vote_2)
by genderid, sort: gen p_hat = conservative_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Conservative", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 75) titlegap(2)) ///
	ylabel(0(25)75) ///
	yline(29.12, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_CON_U, replace
restore

*** NDP
preserve
keep if year == 2021 & genderid ~= .
drop if vote_3 == .

tab vote_3

tab genderid vote_3, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen ndp_sum = sum(vote_3)
by genderid, sort: gen p_hat = ndp_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("NDP", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 75) titlegap(2)) ///
	ylabel(0(25)75) ///
	yline(22.66, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_NDP_U, replace
restore

*** BQ
preserve
keep if year == 2021 & genderid ~= .
drop if vote_4 == .

tab vote_4

tab genderid vote_4, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen ndp_sum = sum(vote_4)
by genderid, sort: gen p_hat = ndp_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("BQ", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 75) titlegap(2)) ///
	ylabel(0(25)75) ///
	yline(10.37, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_BQ_U, replace
restore

*** GREEN
preserve
keep if year == 2021 & genderid ~= .
drop if vote_5 == .

tab vote_5

tab genderid vote_5, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen green_sum = sum(vote_5)
by genderid, sort: gen p_hat = green_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Green", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 75) titlegap(2)) ///
	ylabel(0(25)75) ///
	yline(2.82, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_GRN_U, replace
restore

graph combine Vote_LIB_U.gph Vote_CON_U.gph Vote_NDP_U.gph Vote_BQ_U.gph Vote_GRN_U.gph, col(2) ycommon xcommon b1("Gender Identity", size(small)) l1("Percent Intending to Vote for Party", size(small))
graph export "GenderVoteIntention_Unweighted.png", height(2400) width(2000) replace

***********************************************
*** PARTY IDENTIFICATION, LGBTQ RESPONDENTS ***
***********************************************

tab partyid if lgbtq == 1

*** LIBERAL
preserve
keep if year == 2021 & genderid ~= . & lgbtq == 1
drop if pid_1 == .

tab genderid pid_1, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen liberal_sum = sum(pid_1)
by genderid, sort: gen p_hat = liberal_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Liberal", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(32.34, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_LIB_U_LGBTQ, replace
restore

*** CONSERVATIVE
preserve
keep if year == 2021 & genderid ~= . & lgbtq == 1
drop if pid_2 == .

tab genderid pid_2, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen conservative_sum = sum(pid_2)
by genderid, sort: gen p_hat = conservative_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Conservative", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(12.05, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_CON_U_LGBTQ, replace
restore

*** NDP
preserve
keep if year == 2021 & genderid ~= . & lgbtq == 1
drop if pid_3 == .

tab genderid pid_3, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen ndp_sum = sum(pid_3)
by genderid, sort: gen p_hat = ndp_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("NDP", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(28.53, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_NDP_U_LGBTQ, replace
graph export "NDPPID_LGBTQ.png", height(1200) width(1600) replace
restore

*** BQ
preserve
keep if year == 2021 & genderid ~= . & lgbtq == 1
drop if pid_4 == .

tab genderid pid_4, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen ndp_sum = sum(pid_4)
by genderid, sort: gen p_hat = ndp_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("BQ", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(8.81, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_BQ_U_LGBTQ, replace
restore

*** GREEN
preserve
keep if year == 2021 & genderid ~= . & lgbtq == 1
drop if pid_5 == .

tab genderid pid_5, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen green_sum = sum(pid_5)
by genderid, sort: gen p_hat = green_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Green", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(4.60, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_GRN_U_LGBTQ, replace
restore

*** NONE
preserve
keep if year == 2021 & genderid ~= . & lgbtq == 1
drop if pid_7 == .

tab genderid pid_7, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen green_sum = sum(pid_7)
by genderid, sort: gen p_hat = green_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("None", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(11.66, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_NONE_U_LGBTQ, replace
restore

*** COMBINE PARTY IDENTIFICATION GRAPHS
graph combine PID_LIB_U_LGBTQ.gph PID_CON_U_LGBTQ.gph PID_NDP_U_LGBTQ.gph PID_BQ_U_LGBTQ.gph PID_GRN_U_LGBTQ.gph PID_NONE_U_LGBTQ.gph, col(2) ycommon xcommon b1("Gender Identity", size(small)) l1("Percent Identifying with Party", size(small))
graph export "GenderPID_LGBTQ_Unweighted.png", height(2400) width(2000) replace

******************************************
*** VOTE INTENTIONS, LGBTQ RESPONDENTS ***
******************************************

*** LIBERAL
preserve
keep if year == 2021 & genderid ~= . & lgbtq == 1
drop if vote_1 == .

tab genderid vote_1, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen liberal_sum = sum(vote_1)
by genderid, sort: gen p_hat = liberal_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Liberal", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(29.21, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_LIB_U_LGBTQ, replace
restore

*** CONSERVATIVE
preserve
keep if year == 2021 & genderid ~= . & lgbtq == 1
drop if vote_2 == .

tab genderid vote_2, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen conservative_sum = sum(vote_2)
by genderid, sort: gen p_hat = conservative_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Conservative", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(15.42, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_CON_U_LGBTQ, replace
restore

*** NDP
preserve
keep if year == 2021 & genderid ~= . & lgbtq == 1
drop if vote_3 == .

tab genderid vote_3, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen ndp_sum = sum(vote_3)
by genderid, sort: gen p_hat = ndp_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("NDP", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(36.78, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_NDP_U_LGBTQ, replace
restore

*** BQ
preserve
keep if year == 2021 & genderid ~= . & lgbtq == 1
drop if vote_4 == .

tab genderid vote_4, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen ndp_sum = sum(vote_4)
by genderid, sort: gen p_hat = ndp_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("BQ", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(9.40, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_BQ_U_LGBTQ, replace
restore

*** GREEN
preserve
keep if year == 2021 & genderid ~= . & lgbtq == 1
drop if vote_5 == .

tab genderid vote_5, row chi

gen count = 1

by genderid, sort: egen N = sum(count)
by genderid, sort: egen green_sum = sum(vote_5)
by genderid, sort: gen p_hat = green_sum / N
by genderid, sort: gen middle90 = (p_hat + (1.645^2/(2*N))) / (1 + ((1.645^2)/(N)))
by genderid, sort: gen lower90 = middle90 - (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen upper90 = middle90 + (1.645*sqrt((p_hat*(1-p_hat)) + ((1.645^2)/(4*N))))/((sqrt(N)*(1 + ((1.645^2)/N))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*N))) / (1 + ((1.96^2)/(N)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*N))))/((sqrt(N)*(1 + ((1.96^2)/N))))

collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Green", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(5.41, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_GRN_U_LGBTQ, replace
restore

graph combine Vote_LIB_U_LGBTQ.gph Vote_CON_U_LGBTQ.gph Vote_NDP_U_LGBTQ.gph Vote_BQ_U_LGBTQ.gph Vote_GRN_U_LGBTQ.gph, col(2) ycommon xcommon b1("Gender Identity", size(small)) l1("Percent Intending to Vote for Party", size(small))
graph export "GenderVoteIntention_LGBTQ_Unweighted.png", height(2400) width(2000) replace

